<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  
  <meta name="description" content="0.环境准备本文基于下文，使用 IDEA 调试：

JavaWeb MySQL 实现登录验证:https://eslsuwen.github.io/2019/06/03/Java-Login/

1.项目修改修改 login.jsp，使其能传递注册消息123456789101112131415161">
  

  
  
  
  
  
  
  <title>JavaWeb Filter 实现登录注册过滤 | Suwen</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="0.环境准备本文基于下文，使用 IDEA 调试：  JavaWeb MySQL 实现登录验证:https://eslsuwen.github.io/2019/06/03/Java-Login/  1.项目修改修改 login.jsp，使其能传递注册消息123456789101112131415161718192021222324252627&amp;lt;!-- contact-form --&amp;gt;&amp;l">
<meta name="keywords" content="java,mysql">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaWeb Filter 实现登录注册过滤">
<meta property="og:url" content="http://yoursite.com/2019/06/03/Java-Filter/index.html">
<meta property="og:site_name" content="Suwen">
<meta property="og:description" content="0.环境准备本文基于下文，使用 IDEA 调试：  JavaWeb MySQL 实现登录验证:https://eslsuwen.github.io/2019/06/03/Java-Login/  1.项目修改修改 login.jsp，使其能传递注册消息123456789101112131415161718192021222324252627&amp;lt;!-- contact-form --&amp;gt;&amp;l">
<meta property="og:locale" content="CHS">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/0.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/1.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/2.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/3.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/5.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/6.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/2.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/4.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/7.png">
<meta property="og:image" content="http://yoursite.com/2019/06/03/Java-Filter/8.png">
<meta property="og:updated_time" content="2019-06-03T03:44:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaWeb Filter 实现登录注册过滤">
<meta name="twitter:description" content="0.环境准备本文基于下文，使用 IDEA 调试：  JavaWeb MySQL 实现登录验证:https://eslsuwen.github.io/2019/06/03/Java-Login/  1.项目修改修改 login.jsp，使其能传递注册消息123456789101112131415161718192021222324252627&amp;lt;!-- contact-form --&amp;gt;&amp;l">
<meta name="twitter:image" content="http://yoursite.com/2019/06/03/Java-Filter/0.png">
  
  
    <link rel="icon" href="/css/images/favicon.ico">
  
  <link rel="stylesheet" href="/css/style.css">
  

  
  <!-- baidu webmaster push -->
  <script src="//push.zhanzhang.baidu.com/push.js"></script>
</head></html>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/" title="Suwen" rel="home">Suwen</a>
      </h1>
      
        <h2 class="site-description">
          <a href="/" id="subtitle">never settle</a>
        </h2>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/archives">Archives</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/shigure">Shigure</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-Java-Filter" class="post-Java-Filter post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      JavaWeb Filter 实现登录注册过滤
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="http://yoursite.com/2019/06/03/Java-Filter/" data-id="ck2tq8ur40002sj77ux7iluyq" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h1 id="0-环境准备"><a href="#0-环境准备" class="headerlink" title="0.环境准备"></a>0.环境准备</h1><p>本文基于下文，使用 IDEA 调试：</p>
<blockquote>
<p>JavaWeb MySQL 实现登录验证:<a href="https://eslsuwen.github.io/2019/06/03/Java-Login/" target="_blank" rel="noopener">https://eslsuwen.github.io/2019/06/03/Java-Login/</a></p>
</blockquote>
<h1 id="1-项目修改"><a href="#1-项目修改" class="headerlink" title="1.项目修改"></a>1.项目修改</h1><h3 id="修改-login-jsp，使其能传递注册消息"><a href="#修改-login-jsp，使其能传递注册消息" class="headerlink" title="修改 login.jsp，使其能传递注册消息"></a>修改 login.jsp，使其能传递注册消息</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- contact-form --&gt;</span><br><span class="line">&lt;div class=&quot;message warning&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;inset&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;login-head&quot;&gt;</span><br><span class="line">            &lt;h1&gt;请先登录&lt;/h1&gt;</span><br><span class="line">            &lt;div class=&quot;alert-close&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">        &lt;form action=&quot;login&quot; method=&quot;POST&quot; name=&quot;login&quot;&gt;</span><br><span class="line">            &lt;li&gt;</span><br><span class="line">                &lt;input type=&quot;text&quot; class=&quot;text&quot; name=&quot;username&quot; value=&quot;username&quot; onfocus=&quot;this.value = &apos;&apos;;&quot;</span><br><span class="line">                       onblur=&quot;if (this.value == &apos;&apos;) &#123;this.value = &apos;用户名&apos;;&#125;&quot;&gt;&lt;a href=&quot;#&quot; class=&quot; icon user&quot;&gt;&lt;/a&gt;</span><br><span class="line">            &lt;/li&gt;</span><br><span class="line">            &lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;</span><br><span class="line">            &lt;li&gt;</span><br><span class="line">                &lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;password&quot; onfocus=&quot;this.value = &apos;&apos;;&quot;</span><br><span class="line">                       onblur=&quot;if (this.value == &apos;&apos;) &#123;this.value = &apos;Password&apos;;&#125;&quot;&gt; &lt;a href=&quot;#&quot; class=&quot;icon lock&quot;&gt;&lt;/a&gt;</span><br><span class="line">            &lt;/li&gt;</span><br><span class="line">            &lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;</span><br><span class="line">            &lt;div class=&quot;submit&quot;&gt;</span><br><span class="line">                &lt;input type=&quot;submit&quot; value=&quot;登录&quot;&gt;</span><br><span class="line">                &lt;input type=&quot;submit&quot; value=&quot;注册&quot; name=&quot;isSignUp&quot;&gt;</span><br><span class="line">                &lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;/form&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>
<h3 id="修改-login-servlet-使其能接收注册消息"><a href="#修改-login-servlet-使其能接收注册消息" class="headerlink" title="修改 login servlet, 使其能接收注册消息"></a>修改 login servlet, 使其能接收注册消息</h3><ul>
<li>先判断注册消息</li>
<li>再验证登录信息</li>
<li>成功登录后通过 session 将登录状态改为 true</li>
<li>验证失败发送提示消息，并跳转回 login.jsp 页面</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">protected void doPost(HttpServletRequest request, HttpServletResponse response)</span><br><span class="line">            throws ServletException, IOException &#123;</span><br><span class="line">        // 设置编码格式，可以防止乱码出现</span><br><span class="line">        response.setContentType(&quot;text/html; charset=utf-8&quot;);</span><br><span class="line">        response.setCharacterEncoding(&quot;utf-8&quot;);</span><br><span class="line">        request.setCharacterEncoding(&quot;utf-8&quot;);</span><br><span class="line">        // 获取用户提交过来的用户名和密码，是通过表单中input的name属性获取的</span><br><span class="line">        String username = request.getParameter(&quot;username&quot;);</span><br><span class="line">        String password = request.getParameter(&quot;password&quot;);</span><br><span class="line">        String isSignUp = request.getParameter(&quot;isSignUp&quot;);</span><br><span class="line">        PrintWriter out = response.getWriter();</span><br><span class="line">        System.out.println(username + &quot;: &quot; + password + &quot; isSignUp: &quot; + isSignUp);</span><br><span class="line">        // 获取输出流，用户显示信息，也可以用转发或者重定向</span><br><span class="line">        PrintWriter pw = response.getWriter();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        //注册验证</span><br><span class="line">        if (&quot;注册&quot;.equals(isSignUp)) &#123;</span><br><span class="line">            try &#123;</span><br><span class="line">                JdbcOp jdbc = new JdbcOp();</span><br><span class="line">                jdbc.open(&quot;usersdb&quot;);</span><br><span class="line">                ResultSet rs = jdbc.executeQuery(&quot;select * from users&quot;);</span><br><span class="line"></span><br><span class="line">                while (rs.next()) &#123;</span><br><span class="line">                    String nm = rs.getString(&quot;name&quot;);</span><br><span class="line">                    String pd = rs.getString(&quot;password&quot;);</span><br><span class="line">                    System.out.println(&quot;username: &quot; + nm + &quot; password: &quot; + pd);</span><br><span class="line"></span><br><span class="line">                    if (nm.equals(username)) &#123;</span><br><span class="line">                        out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;该账户已存在！请重新注册！&apos;);window.location.href=&apos;test.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">                        out.flush();</span><br><span class="line">                        out.close();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                //进行注册操作</span><br><span class="line">                int i = jdbc.executeUpdate(&quot;insert into users values(&apos;&quot; + username + &quot;&apos;,&apos;&quot; + password + &quot;&apos;)&quot;);</span><br><span class="line">                System.out.println(i);</span><br><span class="line">                if (i == 0) &#123;</span><br><span class="line">                    out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;账户创建失败！请重新注册！&apos;);window.location.href=&apos;index.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">                    out.flush();</span><br><span class="line">                    out.close();</span><br><span class="line">                &#125; else &#123;</span><br><span class="line">                    out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;该账号已成功注册！请牢记密码！&apos;);window.location.href=&apos;test.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">                    out.flush();</span><br><span class="line">                    out.close();</span><br><span class="line">                &#125;</span><br><span class="line">                jdbc.close();</span><br><span class="line">            &#125; catch (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            //登录验证</span><br><span class="line">            try &#123;</span><br><span class="line">                JdbcOp jdbc = new JdbcOp();</span><br><span class="line">                jdbc.open(&quot;usersdb&quot;);</span><br><span class="line">                ResultSet rs = jdbc.executeQuery(&quot;select * from users&quot;);</span><br><span class="line"></span><br><span class="line">                while (rs.next()) &#123;</span><br><span class="line">                    String nm = rs.getString(&quot;name&quot;);</span><br><span class="line">                    String pd = rs.getString(&quot;password&quot;);</span><br><span class="line">                    System.out.println(&quot;username: &quot; + nm + &quot; password: &quot; + pd);</span><br><span class="line"></span><br><span class="line">                    if (nm.equals(username) &amp;&amp; pd.equals(password)) &#123;</span><br><span class="line">                        HttpSession session = request.getSession();</span><br><span class="line">                        //将数据存储到session中</span><br><span class="line">                        session.setAttribute(&quot;isLogin&quot;, &quot;true&quot;);</span><br><span class="line">                        session.setAttribute(&quot;username&quot;, username);</span><br><span class="line"></span><br><span class="line">                        out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;You login successful  ! 你已成功登录！out.print&apos;);window.location.href=&apos;test.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">                        out.flush();</span><br><span class="line">                        out.close();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                jdbc.close();</span><br><span class="line">            &#125; catch (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            pw.write(&quot;login fail&quot;);</span><br><span class="line">            out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;please login first 登陆信息输入有误！&apos;);window.location.href=&apos;index.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">            out.flush();</span><br><span class="line">            out.close();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h1 id="3-创建-login-filter-文件"><a href="#3-创建-login-filter-文件" class="headerlink" title="3.创建 login filter 文件"></a>3.创建 login filter 文件</h1><ul>
<li>过滤前先判断是否是需要放行的页面</li>
<li>然后判断登录状态，true 放行，false 强制跳转到 login.jsp</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">package login;</span><br><span class="line"></span><br><span class="line">import javax.servlet.*;</span><br><span class="line">import javax.servlet.annotation.WebFilter;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line">import javax.servlet.http.HttpSession;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.io.PrintWriter;</span><br><span class="line">import java.net.URLEncoder;</span><br><span class="line"></span><br><span class="line">@WebFilter(filterName = &quot;LoginFilter&quot;, urlPatterns = &quot;/*&quot;)</span><br><span class="line">public class LoginFilter implements Filter &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 需要排除的页面</span><br><span class="line">     */</span><br><span class="line">    private String excludedPages;</span><br><span class="line">    private String[] excludedPageArray = &#123;&quot;/index.jsp&quot;, &quot;/login.jsp&quot;, &quot;/login&quot;&#125;;</span><br><span class="line"></span><br><span class="line">    public void destroy() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException &#123;</span><br><span class="line">        HttpServletRequest request = (HttpServletRequest) req;</span><br><span class="line">        HttpServletResponse response = (HttpServletResponse) resp;</span><br><span class="line"></span><br><span class="line">        response.setCharacterEncoding(&quot;UTF-8&quot;);</span><br><span class="line">        response.setContentType(&quot;text/html;charset=UTF-8&quot;);</span><br><span class="line">        //使用request对象的getSession()获取session，如果session不存在则创建一个</span><br><span class="line">        HttpSession session = request.getSession();</span><br><span class="line"></span><br><span class="line">        //获取session的Id</span><br><span class="line">        String sessionId = session.getId();</span><br><span class="line">        //判断session是不是新创建的</span><br><span class="line">        if (session.isNew()) &#123;</span><br><span class="line">            //将数据存储到session中</span><br><span class="line">            session.setAttribute(&quot;isLogin&quot;, &quot;false&quot;);</span><br><span class="line">            response.getWriter().print(&quot;session generate successfully, the ID of session: &quot; + sessionId);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            response.getWriter().print(&quot;sever has existed the session, the ID of session: &quot; + sessionId);</span><br><span class="line">        &#125;</span><br><span class="line">        Object loginFlag = session.getAttribute(&quot;isLogin&quot;);     //(&quot;isLogin&quot;);</span><br><span class="line">        response.getWriter().print(&quot;  session.isLogin: &quot; + loginFlag);</span><br><span class="line"></span><br><span class="line">        boolean isExcludedPage = false;</span><br><span class="line">        response.getWriter().print(&quot;  current.url=&quot; + request.getServletPath()+&quot; current.user=&quot;+session.getAttribute(&quot;username&quot;));</span><br><span class="line">        for (</span><br><span class="line">                String page : excludedPageArray) &#123;// 判断是否在过滤 url 之外</span><br><span class="line">            if (request.getServletPath().equals(page)) &#123;</span><br><span class="line">                isExcludedPage = true;</span><br><span class="line">                break;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        if (isExcludedPage) &#123;// 在过滤 url 之外</span><br><span class="line">            chain.doFilter(request, response);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            if (loginFlag == &quot;false&quot;) &#123;</span><br><span class="line">                //没有登录</span><br><span class="line">                PrintWriter out = response.getWriter();</span><br><span class="line">                out.print(&quot;&lt;script language=&apos;javascript&apos;&gt;alert(&apos;Please login first! 请先登录！out.print&apos;);window.location.href=&apos;index.jsp&apos;;&lt;/script&gt;&quot;);</span><br><span class="line">                out.flush();</span><br><span class="line">                out.close();</span><br><span class="line">            &#125;</span><br><span class="line">            chain.doFilter(request, response);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void init(FilterConfig config) throws ServletException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="4-调试"><a href="#4-调试" class="headerlink" title="4.调试"></a>4.调试</h1><p>开户 filter 后，访问其他页面会被阻止并返回登录：<br><img src="/2019/06/03/Java-Filter/0.png" alt="0"><br><img src="/2019/06/03/Java-Filter/1.png" alt="1"></p>
<p>成功登录：<br><img src="/2019/06/03/Java-Filter/2.png" alt="2"><br><img src="/2019/06/03/Java-Filter/3.png" alt="3"></p>
<p>登录验证失败：<br><img src="/2019/06/03/Java-Filter/5.png" alt="5"><br><img src="/2019/06/03/Java-Filter/6.png" alt="6"></p>
<p>注册判断用户名是否被占用：<br><img src="/2019/06/03/Java-Filter/2.png" alt="2"><br><img src="/2019/06/03/Java-Filter/4.png" alt="4"></p>
<p>成功注册：<br><img src="/2019/06/03/Java-Filter/7.png" alt="7"><br><img src="/2019/06/03/Java-Filter/8.png" alt="8"></p>
<h1 id="5-附"><a href="#5-附" class="headerlink" title="5.附"></a>5.附</h1><p>完整项目及其他相关资源下载：</p>
<blockquote>
<p><a href="https://github.com/EslSuwen/JavaWeb" target="_blank" rel="noopener">https://github.com/EslSuwen/JavaWeb</a></p>
</blockquote>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/2019/06/03/Java-Filter/">
    <time datetime="2019-06-03T02:13:19.000Z" class="entry-date">
        2019-06-03
    </time>
</a>
    
  <span class="article-delim">&#8226;</span>
  <div class="article-category">
  <a class="article-category-link" href="/categories/JAVA企业开发框架/">JAVA企业开发框架</a>
  </div>

    
  <span class="article-delim">&#8226;</span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/mysql/">mysql</a></li></ul>

    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/2019/06/09/当前云平台发展现状/" rel="prev"><span class="meta-nav">←</span> 当前云平台发展现状</a></span>
    
    
        <span class="nav-next"><a href="/2019/06/03/Java-Login/" rel="next">JavaWeb MySQL 实现登录验证 <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside class="widget">
  <h3 class="widget-title">ABOUT ME</h3>
  <div class="widget-content">
    <p>E-MAIL:esllovesn@gmail.com</p>
    <p>Github: <a href="https://github.com/EslSuwen" target="_blank">EslSuwen</a></p>
    <p>QQ：<a href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=577014284&website=www.oicqzone.com" target="_blank">577014284</a></p>
    <a href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=577014284&website=www.oicqzone.com" target="_blank"><img src="http://q.qlogo.cn/headimg_dl?bs=qq&dst_uin=577014284&src_uin=www.feifeiboke.com&fid=blog&spec=100" alt="hfl"></a>
    <p><strong>#全栈工程师</strong></p>
    <p>“精通 C++/Java/HTML/CSS/JS 等单词的拼写。”</p>
  </div>
</aside>
  
    
<aside class="widget">
  <h3 class="widget-title">Music</h3>
  <div class="widget-content">
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="280" height="86" src="//music.163.com/outchain/player?type=2&id=101092&auto=0&height=66"></iframe>
  </div>
</aside>

  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/2019/11/11/conceptual-model/">conceptual-model</a>
          </li>
        
          <li>
            <a href="/2019/09/01/ubuntu-install/">Windows 10 &amp; Linux 的艰难历程</a>
          </li>
        
          <li>
            <a href="/2019/06/23/2017gdp-rank/">2017 世界主要国家和地区 GDP 排名</a>
          </li>
        
          <li>
            <a href="/2019/06/09/当前云平台发展现状/">当前云平台发展现状</a>
          </li>
        
          <li>
            <a href="/2019/06/03/Java-Filter/">JavaWeb Filter 实现登录注册过滤</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  <aside class="widget">
    <h3 class="widget-title">Categories</h3>
    <div class="widget-content">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/CODE/">CODE</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/DATABASE/">DATABASE</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA企业开发框架/">JAVA企业开发框架</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/TEST/">TEST</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/TOOL/">TOOL</a><span class="category-list-count">5</span></li></ul>
    </div>
  </aside>

  
    
  <aside class="widget">
    <h3 class="widget-title">Tags</h3>
    <div class="widget-content">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/c/">c++</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cloud-platforms/">cloud platforms</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/eclipse/">eclipse</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/github/">github</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jsp/">jsp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/markdown/">markdown</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tableau/">tableau</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/test/">test</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/">vscode</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/windows/">windows</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </aside>

  
    
  <aside class="widget">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget-content tagcloud">
      <a href="/tags/c/" style="font-size: 10px;">c++</a> <a href="/tags/cloud-platforms/" style="font-size: 10px;">cloud platforms</a> <a href="/tags/eclipse/" style="font-size: 10px;">eclipse</a> <a href="/tags/git/" style="font-size: 13.33px;">git</a> <a href="/tags/github/" style="font-size: 10px;">github</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/jsp/" style="font-size: 10px;">jsp</a> <a href="/tags/linux/" style="font-size: 10px;">linux</a> <a href="/tags/markdown/" style="font-size: 13.33px;">markdown</a> <a href="/tags/mysql/" style="font-size: 20px;">mysql</a> <a href="/tags/tableau/" style="font-size: 10px;">tableau</a> <a href="/tags/test/" style="font-size: 16.67px;">test</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/windows/" style="font-size: 10px;">windows</a>
    </div>
  </aside>

  
    
  <aside class="widget">
    <h3 class="widget-title">Archives</h3>
    <div class="widget-content">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">May 2019</a><span class="archive-list-count">11</span></li></ul>
    </div>
  </aside>

  
    <script>
    // sub title
    var subtitleList = [
        "『卑鄙是卑鄙者的通行证，高尚是高尚者的墓志铭。』——北岛",
        "『所有模型都是错的，但其中一些是有用的。』",
        "『程序员的双手是魔术师的双手，他们把枯燥无味的代码变成了丰富多彩的软件。』——《疯狂的程序员》",
        "『质疑计算机能不能思考，就相当于质疑潜艇能不能游泳。』——Edsger Dijkstra",
        "『人们认为计算机科学是天才的艺术，但是实际情况相反，只是许多人在其它人基础上做一些东西，就像一面由石子垒成的墙。』——高德纳",
        "『Stay young. Stay simple. Always naive!』",
        "『你们媒体千万要记着，不要「见得风，是得雨」。接到消息，你们媒体本身也要判断。』——长者",
        "『假设这些完全无中生有的东西，你再帮他说一遍，你等于......你也有责任吧？』——长者",
        "『刚才你问我啊，我可以回答你一句「无可奉告」。』——长者",
        "『我告诉你们我是身经百战了，见得多了！』——长者",
        "『美国的华莱士，比你们不知要高到哪里去了，我跟他谈笑风生。』——长者",
        "『我有这个必要告诉你们一点，人生的经验。』——长者",
        "『我没有说要钦定，没有任何这个意思。』——长者",
        "『你们啊，不要想喜欢弄个大新闻，说现在已经钦定了，就把我批判一番。』——长者",
        "『一个人的命运啊，当然要靠自我奋斗，但是也要考虑历史的进程。』——长者",
        "『我说另请高明吧，我实在也不是谦虚。』——长者",
        "『苟利国家生死以，岂因祸福避趋之。』——林则徐",
        "『敢同恶鬼争高下，不向霸王让寸分。』——毛泽东",
        "『我生不为逐鹿来，都门懒筑黄金台。状元百官都如狗，总是刀下觳觫材。』——张献忠",
        "『我自成佛自度我，佛度凡尘我度佛。』",
        "『龙吟九天，一曲焚尘；苍山听雪，归时余几人。』",
        "『人最大的痛苦就是说一些自己都不相信的话。』",
        "『燃琴为薪，铸银矢以射白羽；斑音成竹，裁短笛而驭青龙。』",
        "『我们在人生的每个阶段，能影响多少人，就影响多少人，哪怕只能影响一个人，也好过不影响。——罗永浩",
        "『每个人心中都有一个死小孩。』——《龙族II》",
        "『前面是山，我们就爬山；前面是海，我们就渡海；前面是皇宫，我们就开炮！』——《龙族前传》",
        "『不要习惯了黑暗就为黑暗辩护；不要为自己的苟且而得意；不要嘲讽那些比自己更勇敢热情的人们。我们可以卑微如尘土，不可扭曲如蛆虫。』——季业",
        "『若无力驾驭，自由便是负担。",
        "『若能避开猛烈的狂喜，自然不会有悲痛来袭。』  ——太宰治 《人间失格》",


    ];

    document.addEventListener('DOMContentLoaded', evt => {
        var i = Math.floor(Math.random() * subtitleList.length);
        document.querySelector('#subtitle').innerText = subtitleList[i];
    });

    //Kill XP and IE8
    (function(){
        var ua = navigator.userAgent;
        var res = /Windows NT (\d+\.\d+)/.exec(ua);
        var xpOrLower = res && JSON.parse(res[1]) < 6;
        res = /MSIE (\d+\.\d+)/.exec(ua);
        var ie8OrLower = res && JSON.parse(res[1]) < 9;
        if(xpOrLower || ie8OrLower) {
            alert('请不要用XP及之前的Windows系统，和IE8及之前的IE浏览器访问本站！');
            location.href = "about:blank";
        }
        var fromBaiduSE = /^https?:\/\/www.baidu.com/.test(document.referrer);
        if(fromBaiduSE) {
            alert('检测到你还在使用百度搜索，作为一个程序员，这是一种自暴自弃！\n\n做不作恶的程序员，从不用百度开始！');
            location.href = "about:blank";
        }
    })();
</script>
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2019 Suwen
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>

<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/assets/tororo.model.json"},"display":{"position":"right","width":250,"height":400},"mobile":{"show":true},"log":false});</script></body>
</html>